/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package perso;

import carte.Carte;
import ctrl.command.CmdKill;
import ctrl.command.CmdMove;
import ctrl.command.Command;
import java.util.ArrayList;

/**
 *
 * @author Vincent
 */
public class Chef extends PersonnageTruand {

    private static volatile Chef instance = null;

    private Chef(int id, Carte carte) {
        super(id, carte);
        setImage("/images/chef.jpg");
        name = "Chef";
    }

    public final static Chef getInstance(int id, Carte carte) {

        if (Chef.instance == null) {

            synchronized (Chef.class) {
                if (Chef.instance == null) {
                    Chef.instance = new Chef(id, carte);
                }
            }
        }
        return Chef.instance;
    }

    @Override
    public Command doAction() {
        // CarteTruand map = (CarteTruand)carte;
        Command cmd = new CmdMove(this, px, py);
        int lower = 1;
        int higher = 5;
        int random = (int) (Math.random() * (higher - lower)) + lower;
        int pxDest = px;
        int pyDest = py;
        if (random == 1) {
            if (px + 1 < carte.getPerso().length) {
                pxDest++;
            }
        } else if (random == 2) {
            if (px - 1 > 0) {
                pxDest--;
            }
        } else if (random == 3) {
            if (py - 1 > 0) {
                pyDest--;
            }
        } else if (random == 4) {
            if (py + 1 < carte.getPerso()[0].length) {
                pyDest++;

            }
        }
        Personnage cible = carte.getPerso()[pxDest][pyDest];
        if (cible == null) {
            cmd = new CmdMove(this, pxDest, pyDest);
        } else if (cible instanceof Agent) {
            cmd = new CmdKill(cible);
        } else if (cible instanceof Truand) {
            if (((Truand) cible).isTaupe) {
                cmd = new CmdKill(cible);
            }
        }

        return cmd;
    }

    @Override
    public void convert(ArrayList<Personnage> listPerso) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
